import Vue from 'vue'
import Router from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'
import Hello from '../components/Hello/Hello'
import Welcome from '../components/Welcome'
import Ndt from '../components/License/Ndt' 


Vue.use(Router)

const router = new Router({
    routes: [
        { path: '/', redirect: '/login' },
        { path: '/login', component: Login },
        {
            path: '/home',
            component: Home,
            redirect: '/welcome',
            children: [
                { path: '/hello', component: Hello },
                { path: '/welcome', component:Welcome },
                { path: '/ndt', component:Ndt }


            ]
        },
    ]
})

// 挂载路由守卫
router.beforeEach((to, from, next) => {
    // to将要访问的路径
    // from代表从哪个路径跳转而来
    // next是一个函数，next()直接放行，next('/login')强制跳转到login页面

    // 如果访问login页面，直接放行
    if (to.path === '/login') return next()
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next()
})

export default router